import f5.bigip

# openFile 指定具体的配置对象的文件
openFile = open( '/Users/k.ji/vscode/PycharmProjects/F5-test/f5config.txt' )
initConfig = openFile.read()
readConfig = initConfig.split('\n')

#修改下面的ip，用户名和密码。 如果需要对多个设备做配置，可以配置多个mgmt.. 方便需要对多个设备做相同操作。
mgmtA = f5.bigip.ManagementRoot('ltm mgmt ip','username','password',token='True',debug='True')
mgmtA.debug = True

#创建文件中的pool 和 member 
def createPoolwithMem_(mgmt,readConfig):
	tm_pool = mgmt.tm.ltm.pools.pool
	configLenth = len(readConfig)
	for i in range(configLenth):
		if 'pool' ==  readConfig[i].split(" ")[0]:
			poolmemList = readConfig[i].split(" ")
			poolName = poolmemList[1]
			tm_pool.create( name = poolName, partition = 'Common', monitor = '/Common/tcp' )
			poolLoad = mgmt.tm.ltm.pools.pool.load( partition='Common', name = poolName )
			for mem in poolmemList[2:-1]:
				poolLoad.members_s.members.create( partition='Common', name = mem.split("-")[0] )

#删除文件中的pool 和 member 
def deletePoolwithMem_(mgmt,readConfig):
	tm_pool = mgmt.tm.ltm.pools.pool
	configLenth = len(readConfig)
	for i in range(configLenth):
		if 'pool' ==  readConfig[i].split(" ")[0]:
			poolmemList = readConfig[i].split(" ")
			poolName = poolmemList[1]
			deletePool = tm_pool.load( name = poolName, partition = 'Common' )
			deletePool.delete()

#创建文件中的vs
def createVS_(mgmt,readConfig):
	tm_vs = mgmt.tm.ltm.virtuals.virtual
	configLenth = len(readConfig)
	for i in range(configLenth):
		if 'virtual' ==  readConfig[i].split(" ")[0]:
			vsList = readConfig[i].split(" ")
			vsName = vsList[1]
			vip = vsList[2]
			vsprotocol = vsList[3]
			vsprofile = vsList[4]
			vspool = vsList[5]
			if vsprofile == 'http':
				tm_vs.create( 
							name = vsName, 
							partition = 'Common',
							destination = vip ,
							ipProtocol = vsprotocol ,
							profiles = [vsprofile] ,
							pool = vspool
							)
			else:
				tm_vs.create( 
							name = vsName, 
							partition = 'Common',
							destination = vip ,
							ipProtocol = vsprotocol ,
							pool = vspool
							)			

#删除文件中的vs 
def deleteVS_(mgmt,readConfig):
	tm_vs = mgmt.tm.ltm.virtuals.virtual
	configLenth = len(readConfig)
	for i in range(configLenth):
		if 'virtual' ==  readConfig[i].split(" ")[0]:
			vsList = readConfig[i].split(" ")
			vsName = vsList[1]
			deleteVS = tm_vs.load( name = vsName, partition = 'Common' )
			deleteVS.delete()

#保存配置
def SaveConfig_(mgmt):
	mgmt.tm.sys.config.exec_cmd('save')

#根据文件里的ena/dis ,更新vs的状态，开启/关闭
def updateVSstatus_(mgmt,readConfig):
	tm_vs = mgmt.tm.ltm.virtuals.virtual
	configLenth = len(readConfig)
	for i in range(configLenth):
		if 'virtual' ==  readConfig[i].split(" ")[0]:
			vsList = readConfig[i].split(" ")
			vsName = vsList[1]
			updateVS = tm_vs.load(name = vsName,partition='Common')
			if vsList[6] == 'dis':
				updateVS.disabled = True
			elif vsList[6] == 'ena':
				updateVS.enabled = True		
			updateVS.update()

#根据文件里的ena/dis ,更新member的状态，开启/关闭
def updateMemstatus_(mgmt,readConfig):
	configLenth = len(readConfig)
	for i in range(configLenth):
		if 'pool' ==  readConfig[i].split(" ")[0]:
			poolmemList = readConfig[i].split(" ")
			poolName = poolmemList[1]
			poolLoad = mgmt.tm.ltm.pools.pool.load( partition='Common', name = poolName )
			for mem in poolmemList[2:-1]:
				updateMem = poolLoad.members_s.members.load( partition='Common', name = mem.split("-")[0] )
				if mem.split("-")[1] == 'dis':
					updateMem.session = 'user-disabled'
				elif mem.split("-")[1] == 'ena':
					updateMem.session = 'user-enabled'
				updateMem.update()

#根据文件里的member来更新 pool里的member ip（采用的是先新增后删减的更新方式保证和文件内的一致性）
def updateMemIP_(mgmt,readConfig):
	configLenth = len(readConfig)
	for i in range(configLenth):
		if 'pool' ==  readConfig[i].split(" ")[0]:
			memINFilesList = readConfig[i].split(" ")
			poolName = memINFilesList[1]
			poolLoad = mgmt.tm.ltm.pools.pool.load( partition='Common', name = poolName )
			memINF5s = poolLoad.members_s.get_collection()
			for memINFile in memINFilesList[2:-1]:
				if not  poolLoad.members_s.members.exists( partition='Common', name = memINFile.split("-")[0] ):
					poolLoad.members_s.members.create( partition='Common', name = memINFile.split("-")[0] )
			for memINF5 in memINF5s:
				if ( (memINF5.name + '-dis') not in memINFilesList ) and ( (memINF5.name + '-ena') not in memINFilesList ):
					deleteMem = poolLoad.members_s.members.load( partition='Common', name = memINF5.name )
					deleteMem.delete()
	
			




